home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / u_man / cat1 / perlsyn.z / perlsyn
Encoding:
Text File  |  2002-10-03  |  29.4 KB  |  859 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlsyn - Perl syntax
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      A Perl script consists of a sequence of declarations and statements.  The
  13.      only things that need to be declared in Perl are report formats and
  14.      subroutines.  See the sections below for more information on those
  15.      declarations.  All uninitialized user-created objects are assumed to
  16.      start with a null or 0 value until they are defined by some explicit
  17.      operation such as assignment.  (Though you can get warnings about the use
  18.      of undefined values if you like.)  The sequence of statements is executed
  19.      just once, unlike in sssseeeedddd and aaaawwwwkkkk scripts, where the sequence of
  20.      statements is executed for each input line.  While this means that you
  21.      must explicitly loop over the lines of your input file (or files), it
  22.      also means you have much more control over which files and which lines
  23.      you look at.  (Actually, I'm lying--it is possible to do an implicit loop
  24.      with either the ----nnnn or ----pppp switch.  It's just not the mandatory default
  25.      like it is in sssseeeedddd and aaaawwwwkkkk.)
  26.  
  27.      DDDDeeeeccccllllaaaarrrraaaattttiiiioooonnnnssss
  28.  
  29.      Perl is, for the most part, a free-form language.  (The only exception to
  30.      this is format declarations, for obvious reasons.) Comments are indicated
  31.      by the "#" character, and extend to the end of the line.  If you attempt
  32.      to use /* */ C-style comments, it will be interpreted either as division
  33.      or pattern matching, depending on the context, and C++ // comments just
  34.      look like a null regular expression, so don't do that.
  35.  
  36.      A declaration can be put anywhere a statement can, but has no effect on
  37.      the execution of the primary sequence of statements--declarations all
  38.      take effect at compile time.  Typically all the declarations are put at
  39.      the beginning or the end of the script.  However, if you're using
  40.      lexically-scoped private variables created with my(), you'll have to make
  41.      sure your format or subroutine definition is within the same block scope
  42.      as the my if you expect to be able to access those private variables.
  43.  
  44.      Declaring a subroutine allows a subroutine name to be used as if it were
  45.      a list operator from that point forward in the program.  You can declare
  46.      a subroutine without defining it by saying sub name, thus:
  47.  
  48.          sub myname;
  49.          $me = myname $0             or die "can't get myname";
  50.  
  51.      Note that it functions as a list operator, not as a unary operator; so be
  52.      careful to use or instead of || in this case.  However, if you were to
  53.      declare the subroutine as sub myname ($), then myname would function as a
  54.      unary operator, so either or or || would work.
  55.  
  56.      Subroutines declarations can also be loaded up with the require statement
  57.      or both loaded and imported into your namespace with a use statement.
  58.      See the _p_e_r_l_m_o_d manpage for details on this.
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  71.  
  72.  
  73.  
  74.      A statement sequence may contain declarations of lexically-scoped
  75.      variables, but apart from declaring a variable name, the declaration acts
  76.      like an ordinary statement, and is elaborated within the sequence of
  77.      statements as if it were an ordinary statement.  That means it actually
  78.      has both compile-time and run-time effects.
  79.  
  80.      SSSSiiiimmmmpppplllleeee ssssttttaaaatttteeeemmmmeeeennnnttttssss
  81.  
  82.      The only kind of simple statement is an expression evaluated for its side
  83.      effects.  Every simple statement must be terminated with a semicolon,
  84.      unless it is the final statement in a block, in which case the semicolon
  85.      is optional.  (A semicolon is still encouraged there if the block takes
  86.      up more than one line, because you may eventually add another line.)
  87.      Note that there are some operators like eval {} and do {} that look like
  88.      compound statements, but aren't (they're just TERMs in an expression),
  89.      and thus need an explicit termination if used as the last item in a
  90.      statement.
  91.  
  92.      Any simple statement may optionally be followed by a _S_I_N_G_L_E modifier,
  93.      just before the terminating semicolon (or block ending).  The possible
  94.      modifiers are:
  95.  
  96.          if EXPR
  97.          unless EXPR
  98.          while EXPR
  99.          until EXPR
  100.          foreach EXPR
  101.  
  102.      The if and unless modifiers have the expected semantics, presuming you're
  103.      a speaker of English.  The foreach modifier is an iterator:  For each
  104.      value in EXPR, it aliases $_ to the value and executes the statement.
  105.      The while and until modifiers have the usual "while loop" semantics
  106.      (conditional evaluated first), except when applied to a do-BLOCK (or to
  107.      the now-deprecated do-SUBROUTINE statement), in which case the block
  108.      executes once before the conditional is evaluated.  This is so that you
  109.      can write loops like:
  110.  
  111.          do {
  112.              $line = <STDIN>;
  113.              ...
  114.          } until $line  eq ".\n";
  115.  
  116.      See the do entry in the _p_e_r_l_f_u_n_c manpage.  Note also that the loop
  117.      control statements described later will _N_O_T work in this construct,
  118.      because modifiers don't take loop labels.  Sorry.  You can always put
  119.      another block inside of it (for next) or around it (for last) to do that
  120.      sort of thing.  For next, just double the braces:
  121.  
  122.          do {{
  123.              next if $x == $y;
  124.              # do something here
  125.          }} until $x++ > $z;
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  137.  
  138.  
  139.  
  140.      For last, you have to be more elaborate:
  141.  
  142.          LOOP: {
  143.                  do {
  144.                      last if $x = $y**2;
  145.                      # do something here
  146.                  } while $x++ <= $z;
  147.          }
  148.  
  149.  
  150.      CCCCoooommmmppppoooouuuunnnndddd ssssttttaaaatttteeeemmmmeeeennnnttttssss
  151.  
  152.      In Perl, a sequence of statements that defines a scope is called a block.
  153.      Sometimes a block is delimited by the file containing it (in the case of
  154.      a required file, or the program as a whole), and sometimes a block is
  155.      delimited by the extent of a string (in the case of an eval).
  156.  
  157.      But generally, a block is delimited by curly brackets, also known as
  158.      braces.  We will call this syntactic construct a BLOCK.
  159.  
  160.      The following compound statements may be used to control flow:
  161.  
  162.          if (EXPR) BLOCK
  163.          if (EXPR) BLOCK else BLOCK
  164.          if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
  165.          LABEL while (EXPR) BLOCK
  166.          LABEL while (EXPR) BLOCK continue BLOCK
  167.          LABEL for (EXPR; EXPR; EXPR) BLOCK
  168.          LABEL foreach VAR (LIST) BLOCK
  169.          LABEL BLOCK continue BLOCK
  170.  
  171.      Note that, unlike C and Pascal, these are defined in terms of BLOCKs, not
  172.      statements.  This means that the curly brackets are _r_e_q_u_i_r_e_d--no dangling
  173.      statements allowed.  If you want to write conditionals without curly
  174.      brackets there are several other ways to do it.  The following all do the
  175.      same thing:
  176.  
  177.          if (!open(FOO)) { die "Can't open $FOO: $!"; }
  178.          die "Can't open $FOO: $!" unless open(FOO);
  179.          open(FOO) or die "Can't open $FOO: $!";     # FOO or bust!
  180.          open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
  181.                              # a bit exotic, that last one
  182.  
  183.      The if statement is straightforward.  Because BLOCKs are always bounded
  184.      by curly brackets, there is never any ambiguity about which if an else
  185.      goes with.  If you use unless in place of if, the sense of the test is
  186.      reversed.
  187.  
  188.      The while statement executes the block as long as the expression is true
  189.      (does not evaluate to the null string ("") or 0 or "0").  The LABEL is
  190.      optional, and if present, consists of an identifier followed by a colon.
  191.      The LABEL identifies the loop for the loop control statements next, last,
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  203.  
  204.  
  205.  
  206.      and redo.  If the LABEL is omitted, the loop control statement refers to
  207.      the innermost enclosing loop.  This may include dynamically looking back
  208.      your call-stack at run time to find the LABEL.  Such desperate behavior
  209.      triggers a warning if you use the ----wwww flag.
  210.  
  211.      If there is a continue BLOCK, it is always executed just before the
  212.      conditional is about to be evaluated again, just like the third part of a
  213.      for loop in C.  Thus it can be used to increment a loop variable, even
  214.      when the loop has been continued via the next statement (which is similar
  215.      to the C continue statement).
  216.  
  217.      LLLLoooooooopppp CCCCoooonnnnttttrrrroooollll
  218.  
  219.      The next command is like the continue statement in C; it starts the next
  220.      iteration of the loop:
  221.  
  222.          LINE: while (<STDIN>) {
  223.              next LINE if /^#/;      # discard comments
  224.              ...
  225.          }
  226.  
  227.      The last command is like the break statement in C (as used in loops); it
  228.      immediately exits the loop in question.  The continue block, if any, is
  229.      not executed:
  230.  
  231.          LINE: while (<STDIN>) {
  232.              last LINE if /^$/;      # exit when done with header
  233.              ...
  234.          }
  235.  
  236.      The redo command restarts the loop block without evaluating the
  237.      conditional again.  The continue block, if any, is _n_o_t executed.  This
  238.      command is normally used by programs that want to lie to themselves about
  239.      what was just input.
  240.  
  241.      For example, when processing a file like /_e_t_c/_t_e_r_m_c_a_p.  If your input
  242.      lines might end in backslashes to indicate continuation, you want to skip
  243.      ahead and get the next record.
  244.  
  245.          while (<>) {
  246.              chomp;
  247.              if (s/\\$//) {
  248.                  $_ .= <>;
  249.                  redo unless eof();
  250.              }
  251.              # now process $_
  252.          }
  253.  
  254.      which is Perl short-hand for the more explicitly written version:
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  269.  
  270.  
  271.  
  272.          LINE: while (defined($line = <ARGV>)) {
  273.              chomp($line);
  274.              if ($line =~ s/\\$//) {
  275.                  $line .= <ARGV>;
  276.                  redo LINE unless eof(); # not eof(ARGV)!
  277.              }
  278.              # now process $line
  279.          }
  280.  
  281.      Note that if there were a continue block on the above code, it would get
  282.      executed even on discarded lines.  This is often used to reset line
  283.      counters or ?pat? one-time matches.
  284.  
  285.          # inspired by :1,$g/fred/s//WILMA/
  286.          while (<>) {
  287.              ?(fred)?    && s//WILMA $1 WILMA/;
  288.              ?(barney)?  && s//BETTY $1 BETTY/;
  289.              ?(homer)?   && s//MARGE $1 MARGE/;
  290.          } continue {
  291.              print "$ARGV $.: $_";
  292.              close ARGV  if eof();           # reset $.
  293.              reset       if eof();           # reset ?pat?
  294.          }
  295.  
  296.      If the word while is replaced by the word until, the sense of the test is
  297.      reversed, but the conditional is still tested before the first iteration.
  298.  
  299.      The loop control statements don't work in an if or unless, since they
  300.      aren't loops.  You can double the braces to make them such, though.
  301.  
  302.          if (/pattern/) {{
  303.              next if /fred/;
  304.              next if /barney/;
  305.              # so something here
  306.          }}
  307.  
  308.      The form while/if BLOCK BLOCK, available in Perl 4, is no longer
  309.      available.   Replace any occurrence of if BLOCK by if (do BLOCK).
  310.  
  311.      FFFFoooorrrr LLLLooooooooppppssss
  312.  
  313.      Perl's C-style for loop works exactly like the corresponding while loop;
  314.      that means that this:
  315.  
  316.          for ($i = 1; $i < 10; $i++) {
  317.              ...
  318.          }
  319.  
  320.      is the same as this:
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  335.  
  336.  
  337.  
  338.          $i = 1;
  339.          while ($i < 10) {
  340.              ...
  341.          } continue {
  342.              $i++;
  343.          }
  344.  
  345.      (There is one minor difference: The first form implies a lexical scope
  346.      for variables declared with my in the initialization expression.)
  347.  
  348.      Besides the normal array index looping, for can lend itself to many other
  349.      interesting applications.  Here's one that avoids the problem you get
  350.      into if you explicitly test for end-of-file on an interactive file
  351.      descriptor causing your program to appear to hang.
  352.  
  353.          $on_a_tty = -t STDIN && -t STDOUT;
  354.          sub prompt { print "yes? " if $on_a_tty }
  355.          for ( prompt(); <STDIN>; prompt() ) {
  356.              # do something
  357.          }
  358.  
  359.  
  360.      FFFFoooorrrreeeeaaaacccchhhh LLLLooooooooppppssss
  361.  
  362.      The foreach loop iterates over a normal list value and sets the variable
  363.      VAR to be each element of the list in turn.  If the variable is preceded
  364.      with the keyword my, then it is lexically scoped, and is therefore
  365.      visible only within the loop.  Otherwise, the variable is implicitly
  366.      local to the loop and regains its former value upon exiting the loop.  If
  367.      the variable was previously declared with my, it uses that variable
  368.      instead of the global one, but it's still localized to the loop.  (Note
  369.      that a lexically scoped variable can cause problems if you have
  370.      subroutine or format declarations within the loop which refer to it.)
  371.  
  372.      The foreach keyword is actually a synonym for the for keyword, so you can
  373.      use foreach for readability or for for brevity.  (Or because the Bourne
  374.      shell is more familiar to you than _c_s_h, so writing for comes more
  375.      naturally.)  If VAR is omitted, $_ is set to each value.  If any element
  376.      of LIST is an lvalue, you can modify it by modifying VAR inside the loop.
  377.      That's because the foreach loop index variable is an implicit alias for
  378.      each item in the list that you're looping over.
  379.  
  380.      If any part of LIST is an array, foreach will get very confused if you
  381.      add or remove elements within the loop body, for example with splice.
  382.      So don't do that.
  383.  
  384.      foreach probably won't do what you expect if VAR is a tied or other
  385.      special variable.   Don't do that either.
  386.  
  387.      Examples:
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  401.  
  402.  
  403.  
  404.          for (@ary) { s/foo/bar/ }
  405.  
  406.          foreach my $elem (@elements) {
  407.              $elem *= 2;
  408.          }
  409.  
  410.          for $count (10,9,8,7,6,5,4,3,2,1,'BOOM') {
  411.              print $count, "\n"; sleep(1);
  412.          }
  413.  
  414.          for (1..15) { print "Merry Christmas\n"; }
  415.  
  416.          foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) {
  417.              print "Item: $item\n";
  418.          }
  419.  
  420.      Here's how a C programmer might code up a particular algorithm in Perl:
  421.  
  422.          for (my $i = 0; $i < @ary1; $i++) {
  423.              for (my $j = 0; $j < @ary2; $j++) {
  424.                  if ($ary1[$i] > $ary2[$j]) {
  425.                      last; # can't go to outer :-(
  426.                  }
  427.                  $ary1[$i] += $ary2[$j];
  428.              }
  429.              # this is where that last takes me
  430.          }
  431.  
  432.      Whereas here's how a Perl programmer more comfortable with the idiom
  433.      might do it:
  434.  
  435.          OUTER: foreach my $wid (@ary1) {
  436.          INNER:   foreach my $jet (@ary2) {
  437.                      next OUTER if $wid > $jet;
  438.                      $wid += $jet;
  439.                   }
  440.                }
  441.  
  442.      See how much easier this is?  It's cleaner, safer, and faster.  It's
  443.      cleaner because it's less noisy.  It's safer because if code gets added
  444.      between the inner and outer loops later on, the new code won't be
  445.      accidentally executed.  The next explicitly iterates the other loop
  446.      rather than merely terminating the inner one.  And it's faster because
  447.      Perl executes a foreach statement more rapidly than it would the
  448.      equivalent for loop.
  449.  
  450.      BBBBaaaassssiiiicccc BBBBLLLLOOOOCCCCKKKKssss aaaannnndddd SSSSwwwwiiiittttcccchhhh SSSSttttaaaatttteeeemmmmeeeennnnttttssss
  451.  
  452.      A BLOCK by itself (labeled or not) is semantically equivalent to a loop
  453.      that executes once.  Thus you can use any of the loop control statements
  454.      in it to leave or restart the block.  (Note that this is _N_O_T true in
  455.      eval{}, sub{}, or contrary to popular belief do{} blocks, which do _N_O_T
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  467.  
  468.  
  469.  
  470.      count as loops.)  The continue block is optional.
  471.  
  472.      The BLOCK construct is particularly nice for doing case structures.
  473.  
  474.          SWITCH: {
  475.              if (/^abc/) { $abc = 1; last SWITCH; }
  476.              if (/^def/) { $def = 1; last SWITCH; }
  477.              if (/^xyz/) { $xyz = 1; last SWITCH; }
  478.              $nothing = 1;
  479.          }
  480.  
  481.      There is no official switch statement in Perl, because there are already
  482.      several ways to write the equivalent.  In addition to the above, you
  483.      could write
  484.  
  485.          SWITCH: {
  486.              $abc = 1, last SWITCH  if /^abc/;
  487.              $def = 1, last SWITCH  if /^def/;
  488.              $xyz = 1, last SWITCH  if /^xyz/;
  489.              $nothing = 1;
  490.          }
  491.  
  492.      (That's actually not as strange as it looks once you realize that you can
  493.      use loop control "operators" within an expression,  That's just the
  494.      normal C comma operator.)
  495.  
  496.      or
  497.  
  498.          SWITCH: {
  499.              /^abc/ && do { $abc = 1; last SWITCH; };
  500.              /^def/ && do { $def = 1; last SWITCH; };
  501.              /^xyz/ && do { $xyz = 1; last SWITCH; };
  502.              $nothing = 1;
  503.          }
  504.  
  505.      or formatted so it stands out more as a "proper" switch statement:
  506.  
  507.          SWITCH: {
  508.              /^abc/      && do {
  509.                                  $abc = 1;
  510.                                  last SWITCH;
  511.                             };
  512.  
  513.              /^def/      && do {
  514.                                  $def = 1;
  515.                                  last SWITCH;
  516.                             };
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  533.  
  534.  
  535.  
  536.              /^xyz/      && do {
  537.                                  $xyz = 1;
  538.                                  last SWITCH;
  539.                              };
  540.              $nothing = 1;
  541.          }
  542.  
  543.      or
  544.  
  545.          SWITCH: {
  546.              /^abc/ and $abc = 1, last SWITCH;
  547.              /^def/ and $def = 1, last SWITCH;
  548.              /^xyz/ and $xyz = 1, last SWITCH;
  549.              $nothing = 1;
  550.          }
  551.  
  552.      or even, horrors,
  553.  
  554.          if (/^abc/)
  555.              { $abc = 1 }
  556.          elsif (/^def/)
  557.              { $def = 1 }
  558.          elsif (/^xyz/)
  559.              { $xyz = 1 }
  560.          else
  561.              { $nothing = 1 }
  562.  
  563.      A common idiom for a switch statement is to use foreach's aliasing to
  564.      make a temporary assignment to $_ for convenient matching:
  565.  
  566.          SWITCH: for ($where) {
  567.                      /In Card Names/     && do { push @flags, '-e'; last; };
  568.                      /Anywhere/          && do { push @flags, '-h'; last; };
  569.                      /In Rulings/        && do {                    last; };
  570.                      die "unknown value for form variable where: `$where'";
  571.                  }
  572.  
  573.      Another interesting approach to a switch statement is arrange for a do
  574.      block to return the proper value:
  575.  
  576.          $amode = do {
  577.              if     ($flag & O_RDONLY) { "r" }       # XXX: isn't this 0?
  578.              elsif  ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
  579.              elsif  ($flag & O_RDWR)   {
  580.                  if ($flag & O_CREAT)  { "w+" }
  581.                  else                  { ($flag & O_APPEND) ? "a+" : "r+" }
  582.              }
  583.          };
  584.  
  585.      Or
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  599.  
  600.  
  601.  
  602.              print do {
  603.                  ($flags & O_WRONLY) ? "write-only"          :
  604.                  ($flags & O_RDWR)   ? "read-write"          :
  605.                                        "read-only";
  606.              };
  607.  
  608.      Or if you are certainly that all the && clauses are true, you can use
  609.      something like this, which "switches" on the value of the HTTP_USER_AGENT
  610.      envariable.
  611.  
  612.          #!/usr/bin/perl
  613.          # pick out jargon file page based on browser
  614.          $dir = 'http://www.wins.uva.nl/~mes/jargon';
  615.          for ($ENV{HTTP_USER_AGENT}) {
  616.              $page  =    /Mac/            && 'm/Macintrash.html'
  617.                       || /Win(dows )?NT/  && 'e/evilandrude.html'
  618.                       || /Win|MSIE|WebTV/ && 'm/MicroslothWindows.html'
  619.                       || /Linux/          && 'l/Linux.html'
  620.                       || /HP-UX/          && 'h/HP-SUX.html'
  621.                       || /SunOS/          && 's/ScumOS.html'
  622.                       ||                     'a/AppendixB.html';
  623.          }
  624.          print "Location: $dir/$page\015\012\015\012";
  625.  
  626.      That kind of switch statement only works when you know the && clauses
  627.      will be true.  If you don't, the previous ?: example should be used.
  628.  
  629.      You might also consider writing a hash instead of synthesizing a switch
  630.      statement.
  631.  
  632.      GGGGoooottttoooo
  633.  
  634.      Although not for the faint of heart, Perl does support a goto statement.
  635.      A loop's LABEL is not actually a valid target for a goto; it's just the
  636.      name of the loop.  There are three forms: goto-LABEL, goto-EXPR, and
  637.      goto-&NAME.
  638.  
  639.      The goto-LABEL form finds the statement labeled with LABEL and resumes
  640.      execution there.  It may not be used to go into any construct that
  641.      requires initialization, such as a subroutine or a foreach loop.  It also
  642.      can't be used to go into a construct that is optimized away.  It can be
  643.      used to go almost anywhere else within the dynamic scope, including out
  644.      of subroutines, but it's usually better to use some other construct such
  645.      as last or die.  The author of Perl has never felt the need to use this
  646.      form of goto (in Perl, that is--C is another matter).
  647.  
  648.      The goto-EXPR form expects a label name, whose scope will be resolved
  649.      dynamically.  This allows for computed gotos per FORTRAN, but isn't
  650.      necessarily recommended if you're optimizing for maintainability:
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  665.  
  666.  
  667.  
  668.          goto ("FOO", "BAR", "GLARCH")[$i];
  669.  
  670.      The goto-&NAME form is highly magical, and substitutes a call to the
  671.      named subroutine for the currently running subroutine.  This is used by
  672.      AUTOLOAD() subroutines that wish to load another subroutine and then
  673.      pretend that the other subroutine had been called in the first place
  674.      (except that any modifications to @_ in the current subroutine are
  675.      propagated to the other subroutine.)  After the goto, not even caller()
  676.      will be able to tell that this routine was called first.
  677.  
  678.      In almost all cases like this, it's usually a far, far better idea to use
  679.      the structured control flow mechanisms of next, last, or redo instead of
  680.      resorting to a goto.  For certain applications, the catch and throw pair
  681.      of eval{} and _d_i_e() for exception processing can also be a prudent
  682.      approach.
  683.  
  684.      PPPPOOOODDDDssss:::: EEEEmmmmbbbbeeeeddddddddeeeedddd DDDDooooccccuuuummmmeeeennnnttttaaaattttiiiioooonnnn
  685.  
  686.      Perl has a mechanism for intermixing documentation with source code.
  687.      While it's expecting the beginning of a new statement, if the compiler
  688.      encounters a line that begins with an equal sign and a word, like this
  689.  
  690.          =head1 Here There Be Pods!
  691.  
  692.      Then that text and all remaining text up through and including a line
  693.      beginning with =cut will be ignored.  The format of the intervening text
  694.      is described in the _p_e_r_l_p_o_d manpage.
  695.  
  696.      This allows you to intermix your source code and your documentation text
  697.      freely, as in
  698.  
  699.          =item snazzle($)
  700.  
  701.          The snazzle() function will behave in the most spectacular
  702.          form that you can possibly imagine, not even excepting
  703.          cybernetic pyrotechnics.
  704.  
  705.          =cut back to the compiler, nuff of this pod stuff!
  706.  
  707.          sub snazzle($) {
  708.              my $thingie = shift;
  709.              .........
  710.          }
  711.  
  712.      Note that pod translators should look at only paragraphs beginning with a
  713.      pod directive (it makes parsing easier), whereas the compiler actually
  714.      knows to look for pod escapes even in the middle of a paragraph.  This
  715.      means that the following secret stuff will be ignored by both the
  716.      compiler and the translators.
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  731.  
  732.  
  733.  
  734.          $a=3;
  735.          =secret stuff
  736.           warn "Neither POD nor CODE!?"
  737.          =cut back
  738.          print "got $a\n";
  739.  
  740.      You probably shouldn't rely upon the warn() being podded out forever.
  741.      Not all pod translators are well-behaved in this regard, and perhaps the
  742.      compiler will become pickier.
  743.  
  744.      One may also use pod directives to quickly comment out a section of code.
  745.  
  746.      PPPPllllaaaaiiiinnnn OOOOlllldddd CCCCoooommmmmmmmeeeennnnttttssss ((((NNNNooootttt!!!!))))
  747.  
  748.      Much like the C preprocessor, Perl can process line directives.  Using
  749.      this, one can control Perl's idea of filenames and line numbers in error
  750.      or warning messages (especially for strings that are processed with
  751.      eval()).  The syntax for this mechanism is the same as for most C
  752.      preprocessors: it matches the regular expression
  753.      /^#\s*line\s+(\d+)\s*(?:\s"([^"]*)")?/ with $1 being the line number for
  754.      the next line, and $2 being the optional filename (specified within
  755.      quotes).
  756.  
  757.      Here are some examples that you should be able to type into your command
  758.      shell:
  759.  
  760.          % perl
  761.          # line 200 "bzzzt"
  762.          # the `#' on the previous line must be the first char on line
  763.          die 'foo';
  764.          __END__
  765.          foo at bzzzt line 201.
  766.  
  767.          % perl
  768.          # line 200 "bzzzt"
  769.          eval qq[\n#line 2001 ""\ndie 'foo']; print $@;
  770.          __END__
  771.          foo at - line 2001.
  772.  
  773.          % perl
  774.          eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@;
  775.          __END__
  776.          foo at foo bar line 200.
  777.  
  778.          % perl
  779.          # line 345 "goop"
  780.          eval "\n#line " . __LINE__ . ' "' . __FILE__ ."\"\ndie 'foo'";
  781.          print $@;
  782.          __END__
  783.          foo at goop line 345.
  784.  
  785.  
  786.  
  787.  
  788.  
  789.                                                                        PPPPaaaaggggeeee 11112222
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.                                                                        PPPPaaaaggggeeee 11113333
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.